﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2008 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2008-8-27 8:54:08
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;

public partial class Controls_COMMODITY_PICListControl : System.Web.UI.UserControl
{
    public event RepeaterCommandEventHandler ItemCommand;
    public int count, pageCount;

    private DataTable ProductPictures
    {
        get
        {
            DataTable productPictures = null;
            if (ViewState["ProductPictures"] != null)
            {
                productPictures = (DataTable)ViewState["ProductPictures"];
            }
            return productPictures;
        }
        set
        {
            ViewState["ProductPictures"] = value;
        }
    }

    private int PageSize
    {
        get
        {
            int pageSize = 0;
            if (ViewState["PageSize"] != null)
            {
                pageSize = Convert.ToInt32(ViewState["PageSize"]);
            }
            return pageSize;
        }
        set
        {
            ViewState["PageSize"] = value;
        }
    }

    private int PageIndex
    {
        get
        {
            int pageIndex = 0;
            if (ViewState["PageIndex"] != null)
            {
                pageIndex = Convert.ToInt32(ViewState["PageIndex"]);
            }
            return pageIndex;
        }
        set
        {
            ViewState["PageIndex"] = value;
        }
    }

    private string  ProductId
    {
        get
        {
            string productId = "";
            if (ViewState["ProductId"] != null)
            {
                productId = ViewState["ProductId"].ToString();
            }
            return productId;
        }
        set
        {
            ViewState["ProductId"] = value;
        }
    }

    public List<int> Selected
    {
        get
        {
            List<int> l = new List<int>();
            foreach (RepeaterItem item in rptCOMMODITY_PIC.Items)
            {
                CheckBox cb = item.FindControl("chkChoose") as CheckBox;
                if (cb != null && cb.Checked)
                {
                    HiddenField fld = item.FindControl("hfCOMMODITY_PIC") as HiddenField;
                    if (fld != null)
                    {
                        int id = Convert.ToInt32(fld.Value);
                        l.Add(id);
                    }
                }
            }
            return l;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public void Bind(int pagesize,int pageindex,string spdm)
    {
        this.PageSize = pagesize;
        this.PageIndex = pageindex;
        this.ProductId = spdm;
        HiddenField1.Value = pagesize.ToString().Trim() + "," + pageindex.ToString().Trim() + "," + spdm;
        DataSet compic = new BLL.COMMODITY_PIC().GetPicList(pagesize, pageindex, " COMMODITY_PIC LEFT JOIN GUIGE1 ON CP_COLOR=GGDM", "CP_ID,CP_CODE,CP_COLOR,CP_PICPATH,CP_PART,CP_DEFAULT,CP_STOP,GGMC", "CP_COLOR", false, " AND CP_CODE='" + spdm + "' ", out pageCount, out count);
        this.ProductPictures = compic.Tables[0];
        rptCOMMODITY_PIC.DataSource = compic.Tables[0].DefaultView;
        rptCOMMODITY_PIC.DataBind();
        
    }

    protected void rptCOMMODITY_PIC_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (!Check.CheckPermission("200300100", "00017"))
        {
            Share.Alert(this.Page, "无权操作"); return;
        }
        if (ItemCommand != null)
            ItemCommand(source, e);
        if (e.CommandName == "del")
        {
            new BLL.COMMODITY_PIC().Delete(Int32.Parse(e.CommandArgument.ToString()));
            string[] arr = HiddenField1.Value.Trim().Split(',');
            Bind(Int32.Parse(arr[0]), Int32.Parse(arr[1]), arr[2]);
        }
        else if (e.CommandName.ToLower() == "upt")
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("update COMMODITY_PIC set CP_DEFAULT=0 where CP_CODE='");
            sb.Append(this.ProductId);
            sb.Append("'|");

            sb.Append("update COMMODITY_PIC set CP_DEFAULT=1 where CP_ID='");
            sb.Append(e.CommandArgument);
            sb.Append("'|");

            string stringSql = sb.ToString();
            char[] charArray = new char[] { '|' };
            string[] sqlCommandArray = stringSql.TrimEnd(charArray).Split(charArray);
            if (new SQLServerDAL.ORDERDOC().Deliver(sqlCommandArray))
            {
                Bind(this.PageSize, this.PageSize, this.ProductId);
            }
        }
    }
    protected void rptCOMMODITY_PIC_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            CheckBox chk = (CheckBox)e.Item.FindControl("CheckBox2");
            if (this.ProductPictures.Rows[e.Item.ItemIndex]["CP_PART"].ToString() == "0")
            {
                chk.Enabled = true;
            }

            Button btnUpdateDefaultPicture = (Button)e.Item.FindControl("btnUpdateDefaultPicture");
            chk.Attributes.Add("onclick", btnUpdateDefaultPicture.ClientID + ".click();");
        }
    }
}
